package com.tanjun.mallservice.web.service.impl;

import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.tanjun.mallservice.apiservice.enums.cate.CateTypeEnum;
import com.tanjun.mallservice.web.orm.entity.MallCate;
import com.tanjun.mallservice.web.orm.mapper.MallCateMapper;
import com.tanjun.mallservice.web.service.MallCateService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jjtan
 * @description 针对表【mall_cate(商品分类)】的数据库操作Service实现
 * @createDate 2024-03-19 15:58:44
 */
@Service
public class MallCateServiceImpl extends ServiceImpl<MallCateMapper, MallCate>
        implements MallCateService {

    /**
     * 查询品类数据
     *
     * @param cateType 品类类型
     * @return 返回所有该品类下的数据
     */
    @Override
    @Cacheable(value = "listCateByType",key = "#cateType")
    public List<MallCate> listCateByType(CateTypeEnum cateType) {
        List<MallCate> list = this.list(QueryChain.of((MallCate.class))
                .eq(MallCate::getCateType, cateType)
                .orderBy(MallCate::getCateLevel).asc()
                        .orderBy(MallCate::getSno).asc()
                        .orderBy(MallCate::getCateId).asc());

        return list;
    }

    @Override
    @CacheEvict(cacheNames = {"getCateById", "listCateByType"}, allEntries = true)
    public void addCate(MallCate mallCate) {
        this.save(mallCate);
    }

    /**
     * 根据id查询品类
     *
     * @param cateId 品类id
     * @return
     */
    @Override
    @Cacheable(value = "getCateById",key = "#cateId")
    public MallCate getCateById(Long cateId) {
        return this.getById(cateId);
    }
}




